

import {EasyACtxHandle} from "./EasyACtxHandle";
import {Cons} from "./EasyACons";
import {Interceptor} from "./Interceptor";
import {Controller} from "./Controller";
import {_} from "../util/UtilTs";

/*
 * 用户业务逻辑
 * 继承EasyACtxHandle 是因为，它本身的方法也是一个处理 ctx 的方法
 */

// TODO 这个类型表达的不太对
export type AnyExtendEasyACtxHandle =  Action ;

export type MethodType = 'all' | 'get' | 'post' | 'put' | 'delete';


export class Action extends EasyACtxHandle{



    name = Cons.never;
    route = ""; //路由路径

    interceptors:Array<Interceptor> = [];

    method:MethodType = 'all';
    controller:Controller; //所在controller

    //控制器产生实例后的相关属性
    associatedProps:{
        interceptors:Array<Interceptor>,
        handles:Array<AnyExtendEasyACtxHandle>,//流水式处理
        routePath:string , //最终的路径
        viewPath:string ,
    } = {
        interceptors:[],
        handles:[],
        routePath:'/',
        viewPath:'',
    };

    static create(name,funcInController){
        const ins = new Action(funcInController);
        ins.name = name;
        ins.route = name;
        return ins;
    }



}


export function ActionBind(props: Partial<Action>) {

    return function (target,key) {
        const clazz = target.constructor;
        if(Controller.isControllerDClazz(target)){
            const clazzFunc = target[key];
            clazz.__init((ins)=>{
                const acName = clazzFunc.name;
                _.set(ins, `tempActionProps.${acName}.props`,props);
            });
        }
        else {
            throw "方法必须写在controller 类 的方法里"
        }
    };
}


